export default {
	data() {
		return {
			validate: {}, //验证对象
			roleSelect: [], // 角色下拉框
			//数据
			id: this.get("addUserid"),
			user: {},
			expertGender: "男", // 性别
			professional_skill_duty: '', // 专业技术职务
			professional_skill_duty_level: '', // 专业技术职务级别
			fatherIndex: 0, //一级菜单index
		}
	},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
	},
	created() { //调用方法
		let that = this;
		// 角色下拉框
		that.getData();
		// console.log(that.roleSelect);

		console.log(that.id)
		// 查询基本信息
		if(that.id != 'null' && that.id) {
			that.$post(
					that.urls.findUser, {
						id: that.id
					}
				)
				.then(function(res) {
					if(res.success) {
						that.user = res.data;
						that.expertGender = res.data.expert_gender;
						that.professional_skill_duty = res.data.professional_skill_duty;
						that.professional_skill_duty_level = res.data.professional_skill_duty_level;
					}
				})
		}

	},
	mounted() {
    jQuery.validator.addMethod("isMobile", function(value, element) {
      let length = value.length;
      let mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
      return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写手机号码");
		//加载表单验证
		this.validate = $("#userForm").validate({
			ignore: [],
			rules: {
				expertName: { // 名称
					required: true,
					maxlength: 10
				},
				userAccount: { // 身份证
					required: true,
					maxlength: 18,
					minlength: 18
				},
				email: { // 邮箱
					required: true,
					maxlength: 32,
          email: true
				},
				expertGender: { // 性别
					required: true,
				},
				roleId: { // 角色
					required: true,
				},
				phoneNumber: { // 手机
					required: true,
          isMobile : true
				},
        userPassword: { // 手机
          required: true,
          maxlength: 16,
          minlength: 6
        },


			},
			messages: {
				expertName: { // 名称
					required: "请输入名称",
					maxlength: "请输入长度小于10"
				},
				userAccount: { // userAccount
					required: "请输入身份证",
					maxlength: "请输入长度小于19",
					minlength: '最小长度不小于18'
				},
				email: { // 邮箱
					required: "请输入邮箱",
					maxlength: "请输入长度小于32",
          email: '请输入正确的邮箱'
				},
				roleId: { // 角色
					required: "请选择角色",
				},
				phoneNumber: { // 手机号
					required: "请输入手机号",
          isMobile : '请输入正确的手机号码!'
        },
				expertGender: { // 性别
					required: "请选择性别",
				},
        userPassword: { // 密码
          required: "请输入密码",
          maxlength: '密码长度为6-16个',
          minlength: '密码长度为6-16个'
        },

			},
			/*errorElement: "em",
			errorPlacement: function(error, element) {
				error.appendTo(element.parents(".error-box").children(".error-msg"));
			}*/
		});
	},
	methods: { //写函数方法
		// 新增提交
		subUser: function() {
			let that = this;

			let flg = that.validate.form();
			if(flg) {
				let dataObj = {};
				dataObj = that.combinationFormObject(that.validate.currentForm);
        dataObj.theWork = dataObj.userAccount;
        dataObj.roleName = $("#roleName").find("option:selected").text()

        let obj = {"id": that.id,"userPhoneNumber":dataObj.phoneNumber  };
				// 账号查重
				that.$post(
						that.urls.queryHeavy, {
							userAccount: dataObj.userAccount,
							id: that.id
						}
					)
					.then(function(res) {
						if(res.success) {
              layer.msg(res.data, {
                time: 3000, //3s后自动关闭
              });
						} else {

              that.$post(
                that.urls.queryHeavy,
                obj
              )
                .then(function(res) {
                  if(res.success) {
                    layer.msg('手机号码已存在,请重新输入!', {
                      time: 3000, //3s后自动关闭
                    });
                  }else {
                    let url = '';
                    if(that.id) {
                      url = that.urls.editUser
                      dataObj.id = that.id;
                    } else {
                      url = that.urls.addUser
                    }
                    dataObj.expertGender = that.expertGender;
                    that.$post(
                      url,
                      dataObj
                    )
                      .then(function(res) {
                        if(res.success) {

                          layer.msg(res.message, {
                            time: 3000, //3s后自动关闭
                          });
                          that.$router.push({
                            path: "/userList?fatherIndex="+that.fatherIndex,
                          })

                        }
                      })
                  }
                })

							// addExpert end
						} // 判断非空end
					}) // --
			}
		},
		getData: function() {
			let that = this;
			that.$post(
					that.urls.findAllRole, {
						"pageNum": 0,
						"pageSize": 999999
					}
				)
				.then(function(res) {
					if(res.success) {
						that.roleSelect = res.pageInfo.list;
					}
				})
		},



	}

}
